package com.mangguo.tingjian.content.service.impl;

import com.mangguo.tingjian.content.dto.TagCountResult;
import com.mangguo.tingjian.content.entity.DwPlayer;
import com.mangguo.tingjian.content.mapper.DwPlayerMapper;
import com.mangguo.tingjian.content.mapper.TagConfMapper;
import com.mangguo.tingjian.content.service.IDwPlayerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * play同步表 服务实现类
 * </p>
 *
 * @author auto
 * @since 2019-11-24
 */
@Service
@AllArgsConstructor
public class DwPlayerServiceImpl extends ServiceImpl<DwPlayerMapper, DwPlayer> implements IDwPlayerService {

    private final DwPlayerMapper dwPlayerMapper;

    private final TagConfMapper tagConfMapper;

    @Override
    public Long countPlayBetweenDate(Integer start, Integer end) {
        return dwPlayerMapper.countPlayBetweenDate(start, end);
    }

    @Override
    public Long countPlayAvgBetweenDate(Integer start, Integer end) {
        Long imeiCount = dwPlayerMapper.countPlayImeiBetweenDate(start, end);
        if (imeiCount == null || imeiCount == 0){
            return 0L;
        }
        return dwPlayerMapper.countPlayBetweenDate(start, end) / imeiCount;
    }

    @Override
    public Long sumPlayTimeBetweenDate(Integer start, Integer end) {
        Long sum = dwPlayerMapper.sumPlayTimeBetweenDate(start, end);
        if (sum == null){
            return 0L;
        }
        return sum;
    }

    @Override
    public Long sumPlayAvgTimeBetweenDate(Integer start, Integer end) {
        Long imeiCount = dwPlayerMapper.countPlayImeiBetweenDate(start, end);
        if (imeiCount == null || imeiCount == 0){
            return 0L;
        }
        Long playTime = dwPlayerMapper.sumPlayTimeBetweenDate(start, end);
        if (playTime == null || playTime == 0){
            return 0L;
        }
        return playTime / imeiCount;
    }

    @Override
    public List<TagCountResult> getTagPlayOrder(Long lvl1, Long lvl2, Integer start, Integer end) {
        return dwPlayerMapper.topPlayByLvlBetweenDate(start, end, lvl1, lvl2);
    }
}
